#ifndef TETRIS_H
#define TETRIS_H

#include <QMainWindow>
#include <qpainter.h>

//变量初始化
const int BLOCK_SIZE=30;
const int MARGIN=20;
const int AREA_ROW=25;
const int AREA_COL=20;

enum Direction
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    SPACE
};

struct Border
{
    int u_b;
    int d_b;
    int l_b;
    int r_b;
};

struct block_point
{
    int pos_x;
    int pos_y;
};


QT_BEGIN_NAMESPACE
namespace Ui { class tetris; }
QT_END_NAMESPACE


class tetris : public QMainWindow
{
    Q_OBJECT

public:
    tetris(QWidget *parent = nullptr);
    ~tetris();

    void paintEvent(QPaintEvent *event);//游戏界面
    virtual void timerEvent(QTimerEvent *event);//计时
    virtual void keyPressEvent(QKeyEvent *event);//键盘事件

    enum GameStatus{RUNNING,STOP,PAUSE};
    void InitGame();
    void InitMenu();

    void StartGame();
    void PauseOrContinueGame();
    void GameOver();

    void ResetBlock();//重置方块
    void BlockMove(Direction dir);//方块变动
    void BlockRotate(int block[4][4]);//方块旋转
    void CreateBlock(int block[4][4],int block_id);//产生方块

    void GetBorder(int block[4][4],Border &border);//计算边界
    void ConvertStable(int x,int y);//变换稳定
    bool IsCollide(int x,int y,Direction dir);//是否碰撞

public slots:
    void slotStartGame();
    void slotPauseGame();
    void slotStopGame();
    void slotHelp();

private:
    Ui::tetris *ui;

private:
    int game_area[AREA_ROW][AREA_COL];
    block_point block_pos;
    int cur_block[4][4];
    Border cur_border;
    int next_block[4][4];
    bool isStable;
    int score;
    int game_timer;
    int paint_timer;
    int speed_ms;
    int refresh_ms;
    QMenu *menu;
    QMenu *helpMenu;
    QAction *startGameAction;
    QAction *stopGameAcion;
    QAction *pauseOrcontinueGameAction;
    QAction *helpAction;
    volatile GameStatus gameStatus=GameStatus::STOP;
};


#endif // TETRIS_H
